<%-- 
    Document   : content_response
    Created on : Apr 25, 2012, 9:53:43 PM
    Author     : Asus
--%>

<%@page import="java.util.Map"%>
<%@page import="java.util.Iterator"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="/connect.jspf"%>
<%@page trimDirectiveWhitespaces="true" %>
<%@page import="java.util.HashMap"%>
<%@page import="model.CommentModel, model.UserModel, java.util.Date, util.Utility" %>

<%

    String q = request.getParameter("q");

    if(q.equals("add-comment")) {
        CommentModel commentModel = new CommentModel(con);
        //dapetin user id yang sedang login dari session
		int logged_in_user_id = Integer.parseInt(session.getAttribute("userid").toString());	
                String content = "";	
                int post_id = 0;                
		boolean errorFlag = false;
                HashMap<String, String> errors = new HashMap<String, String>();                
		
		if(request.getParameter("content") != null) {
                    content = request.getParameter("content");			
		}
		if(request.getParameter("post_id") != null) {
                    post_id = Integer.parseInt(request.getParameter("post_id").toString());
		}
		if(request.getParameter("ogged_in_user_id") != null){
                    logged_in_user_id = Integer.parseInt(request.getParameter("logged_in_user_id").toString());
		}	
		
		//validasi komen box	
		if(content.length()==0) {
                    errors.put("content", "Comment box can't be empty!");                    
                    errorFlag = true;
		}
		
		if(!errorFlag) {
			//lolos validasi 	
                    int ret = commentModel.insertComment(logged_in_user_id, post_id, content);                       
                    if(ret>=0) {							
                        //kembalikan response komentar barusan oleh user	
                        ResultSet comment_res = commentModel.getAllComment(post_id);					
                        while(comment_res.next()) {
                            //$comment_datetime = new DateTime($row['datetime'], new DateTimeZone('Asia/Jakarta'));	
                            //$now = new DateTime(null, new DateTimeZone('Asia/Jakarta'));	
                            //String comment_diff_datetime_string = Utility.getDeltaTimeString($comment_datetime, $now);
                            String comment_diff_datetime_string = "";
                            
                            out.write("<div class='content-comment' id='comment" + comment_res.getString("comment_id") + "'>");
                            out.write("<img class='avatar' src='avatar/" + comment_res.getString("avatar") + "'/>");
                            out.write("<h4>" + comment_res.getString("username") + "</h4>");
                            out.write("<p>" + comment_res.getString("content") + "</p>");						
                            out.write("<span name='comment-date'><small>" + comment_diff_datetime_string + "</small></span>");						
                            if(comment_res.getString("username_id").equals(logged_in_user_id + ""))
                                out.write("<input type='button' onclick='delComment(" + comment_res.getString("comment_id") + "');' value='delete' />");
                            out.write("</div>");
                        }												

                }else out.write("error inserting comment ");
		}else{
                    out.write("error \n");
                    Iterator it = errors.entrySet().iterator();
                    while(it.hasNext()) {
                        Map.Entry pairs = (Map.Entry) it.next();
                        out.write(pairs.getValue() + "\n");
                        it.remove();
                    }                      
		}
    }else if(q.equals("del-comment")) {
        CommentModel commentModel = new CommentModel(con);
        //dapetin user id yang sedang login dari session
        int logged_in_user_id = Integer.parseInt(session.getAttribute("userid").toString());		
        int comment_id = 0;
        
        if(request.getParameter("comment_id") != null) {
            comment_id = Integer.parseInt(request.getParameter("comment_id").toString());			
        }		

        //cocokin user yang sedang login sekarang apakah sama dengan user yang ngepos komentar tersebut
        ResultSet comment_res = commentModel.getComment(comment_id);
        while(comment_res.next()) {
            if(comment_res.getInt("username_id") == logged_in_user_id) {
                boolean ret = commentModel.delComment(comment_id);
                if(ret) {
                    out.write(comment_id);
                    System.out.println("DEL : " + comment_id);
                }
            }else{
                out.write("error violation access\n");
            }
        }                
    }

%>